package container

import (
	"fmt"
	"sort"
)

func ReadSorted(m map[string]int) {
	if len(m) == 0 {
		return
	}

	var keys []string
	for key, _ := range m {
		keys = append(keys, key)
	}
	sort.Strings(keys)

	for _, key := range keys {
		fmt.Println(key, m[key])
	}
}

func TestMap() {

	scoreMap := make(map[string]int, 8)
	scoreMap["张三"] = 1
	scoreMap["小明"] = 2

	fmt.Println(scoreMap)

	fmt.Println(scoreMap["小明"])
	fmt.Printf("%T\n", scoreMap)

	userInfo := map[string]string{
		"userName": "fanshuai",
		"password": "123",
	}

	fmt.Println(userInfo)

	value, ok := userInfo["pass"]
	if ok {
		fmt.Println(value)
	} else {
		fmt.Println("userinfo not contains key pass")
	}

	scoreMap = make(map[string]int)
	fmt.Println(len(scoreMap))
	scoreMap["张三"] = 90
	scoreMap["小明"] = 100
	scoreMap["娜扎"] = 60

	for key, value := range scoreMap {
		fmt.Println(key, value)
	}

	fmt.Println(len(scoreMap))

	delete(scoreMap, "小明")
	for key := range scoreMap {
		fmt.Println(key)
	}

	fmt.Println(len(scoreMap))

	ReadSorted(scoreMap)

	var mapSlice = make([]map[string]string, 5)
	mapSlice[0] = make(map[string]string)
	mapSlice[0]["name"] = "小王子"
	mapSlice[0]["password"] = "123456"
	mapSlice[0]["address"] = "沙河"

	for index, value := range mapSlice {
		fmt.Println(index, value)
		fmt.Println(value == nil)
	}

	var sliceMap = make(map[string][]string)
	key := "city"
	sliceValue, ok := sliceMap[key]
	fmt.Println(sliceValue == nil, ok)
	if !ok {
		sliceValue = []string{"北京", "上海"}
		sliceMap[key] = sliceValue
	}

	fmt.Println(sliceMap)

	fmt.Println(WordCount("how do you do how"))

	Print2()
}
